import request from './request'
import { getToken } from '@/utils/auth'
// 正常上传
export const commonUpload = (file) => {
    return request({
        url: '/admin/front/upload',
        method: 'post',
        data: {
            file
        },
        timeout:120000,
        headers: {
            Authorization: 'Bearer ' + getToken(),
            'Content-Type': 'multipart/form-data'
        }
    })
}

/**
 * 根据文件的md5获取未上传完的任务
 * @param identifier 文件md5
 * @returns {Promise<AxiosResponse<any>>}
 */
export const taskInfo = identifier => {
    return request({
        url: `admin/minioUploadTask/taskInfo/${identifier}`,
        method: 'get',
    })
}

/**
 * 初始化一个分片上传任务
 * @param identifier 文件md5
 * @param fileName 文件名称
 * @param totalSize 文件大小
 * @param chunkSize 分块大小
 * @returns {Promise<AxiosResponse<any>>}
 */
export const initTask = ({ identifier, fileName, totalSize, chunkSize }) => {
    return request({
        url: 'admin/minioUploadTask/initTask',
        method: 'post',
        data: { identifier, fileName, totalSize, chunkSize }
    })
}

/**
 * 获取预签名分片上传地址
 * @param identifier 文件md5
 * @param partNumber 分片编号
 * @returns {Promise<AxiosResponse<any>>}
 */
export const preSignUrl = ({ identifier, partNumber }) => {
    return request({
        url: `admin/minioUploadTask/preSignUploadUrl/${identifier}/${partNumber}`,
        method: 'get',
    })
}

/**
 * 合并分片
 * @param identifier
 * @returns {Promise<AxiosResponse<any>>}
 */
export const merge = identifier => {
    return request({
        url: `admin/minioUploadTask/merge/${identifier}`,
        method: 'post',
    })
}

export const partUpload = ({identifier,partNumber}) => {
    return request({
        url: `admin/minioUploadTask/partUpload/${identifier}/${partNumber}`,
        method: 'post',
    })
}